4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 * Special page allowing users with the appropriate permissions to view
22 * and hide revisions. Log items can also be hidden.
25 * @ingroup SpecialPage
28 class SpecialRevisionDelete
extends UnlistedSpecialPage
{
32 /** True if the submit button was clicked, and the form was posted */
38 /** Archive name, for reviewing deleted files */
41 /** Edit token for securing image views against XSS */
44 /** Title object for target parameter */
47 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
50 /** Array of checkbox specs (message, name, deletion bits) */
53 /** Information about the current type */
56 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
60 * Assorted information about each type, needed by the special page.
61 * TODO Move some of this to the list class
63 static $allowedTypes = array(
65 'check-label' => 'revdelete-hide-text',
66 'deletion-bits' => Revision
::DELETED_TEXT
,
67 'success' => 'revdelete-success',
68 'failure' => 'revdelete-failure',
69 'list-class' => 'RevDel_RevisionList',
72 'check-label' => 'revdelete-hide-text',
73 'deletion-bits' => Revision
::DELETED_TEXT
,
74 'success' => 'revdelete-success',
75 'failure' => 'revdelete-failure',
76 'list-class' => 'RevDel_ArchiveList',
79 'check-label' => 'revdelete-hide-image',
80 'deletion-bits' => File
::DELETED_FILE
,
81 'success' => 'revdelete-success',
82 'failure' => 'revdelete-failure',
83 'list-class' => 'RevDel_FileList',
85 'filearchive' => array(
86 'check-label' => 'revdelete-hide-image',
87 'deletion-bits' => File
::DELETED_FILE
,
88 'success' => 'revdelete-success',
89 'failure' => 'revdelete-failure',
90 'list-class' => 'RevDel_ArchivedFileList',
93 'check-label' => 'revdelete-hide-name',
94 'deletion-bits' => LogPage
::DELETED_ACTION
,
95 'success' => 'logdelete-success',
96 'failure' => 'logdelete-failure',
97 'list-class' => 'RevDel_LogList',
101 /** Type map to support old log entries */
102 static $deprecatedTypeMap = array(
103 'oldid' => 'revision',
104 'artimestamp' => 'archive',
105 'oldimage' => 'oldimage',
106 'fileid' => 'filearchive',
107 'logid' => 'logging',
110 public function __construct() {
111 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
114 public function execute( $par ) {
115 global $wgOut, $wgUser, $wgRequest;
116 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
117 $wgOut->permissionRequired( 'deletedhistory' );
119 } else if( wfReadOnly() ) {
120 $wgOut->readOnlyPage();
123 $this->mIsAllowed
= $wgUser->isAllowed('deleterevision'); // for changes
124 $this->skin
= $wgUser->getSkin();
126 $this->outputHeader();
127 $this->submitClicked
= $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
128 # Handle our many different possible input types.
129 $ids = $wgRequest->getVal( 'ids' );
130 if ( !is_null( $ids ) ) {
131 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
132 $this->ids
= explode( ',', $ids );
135 $this->ids
= array_keys( $wgRequest->getArray('ids',array()) );
137 // $this->ids = array_map( 'intval', $this->ids );
138 $this->ids
= array_unique( array_filter( $this->ids
) );
140 if ( $wgRequest->getVal( 'action' ) == 'historysubmit' ) {
141 # For show/hide form submission from history page
142 $this->targetObj
= $GLOBALS['wgTitle'];
143 $this->typeName
= 'revision';
145 $this->typeName
= $wgRequest->getVal( 'type' );
146 $this->targetObj
= Title
::newFromText( $wgRequest->getText( 'target' ) );
149 # For reviewing deleted files...
150 $this->archiveName
= $wgRequest->getVal( 'file' );
151 $this->token
= $wgRequest->getVal( 'token' );
152 if ( $this->archiveName
&& $this->targetObj
) {
153 $this->tryShowFile( $this->archiveName
);
157 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
158 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
162 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
163 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
166 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
168 # If we have revisions, get the title from the first one
169 # since they should all be from the same page. This allows
170 # for more flexibility with page moves...
171 if( $this->typeName
== 'revision' ) {
172 $rev = Revision
::newFromId( $this->ids
[0] );
173 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
176 $this->otherReason
= $wgRequest->getVal( 'wpReason' );
177 # We need a target page!
178 if( is_null($this->targetObj
) ) {
179 $wgOut->addWikiMsg( 'undelete-header' );
182 # Give a link to the logs/hist for this page
183 $this->showConvenienceLinks();
185 # Initialise checkboxes
186 $this->checks
= array(
187 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
188 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
189 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
191 if( $wgUser->isAllowed('suppressrevision') ) {
192 $this->checks
[] = array( 'revdelete-hide-restricted',
193 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
196 # Either submit or create our form
197 if( $this->mIsAllowed
&& $this->submitClicked
) {
198 $this->submit( $wgRequest );
203 $qc = $this->getLogQueryCond();
204 # Show relevant lines from the deletion log
205 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
206 LogEventsList
::showLogExtract( $wgOut, 'delete',
207 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
208 # Show relevant lines from the suppression log
209 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
210 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
211 LogEventsList
::showLogExtract( $wgOut, 'suppress',
212 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
217 * Show some useful links in the subtitle
219 protected function showConvenienceLinks() {
220 global $wgOut, $wgUser, $wgLang;
221 # Give a link to the logs/hist for this page
222 if( $this->targetObj
) {
224 $links[] = $this->skin
->linkKnown(
225 SpecialPage
::getTitleFor( 'Log' ),
226 wfMsgHtml( 'viewpagelogs' ),
228 array( 'page' => $this->targetObj
->getPrefixedText() )
230 if ( $this->targetObj
->getNamespace() != NS_SPECIAL
) {
231 # Give a link to the page history
232 $links[] = $this->skin
->linkKnown(
234 wfMsgHtml( 'pagehist' ),
236 array( 'action' => 'history' )
238 # Link to deleted edits
239 if( $wgUser->isAllowed('undelete') ) {
240 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
241 $links[] = $this->skin
->linkKnown(
243 wfMsgHtml( 'deletedhist' ),
245 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
249 # Logs themselves don't have histories or archived revisions
250 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
255 * Get the condition used for fetching log snippets
257 protected function getLogQueryCond() {
259 // Revision delete logs for these item
260 $conds['log_type'] = array('delete','suppress');
261 $conds['log_action'] = $this->getList()->getLogAction();
262 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
263 $conds['ls_value'] = $this->ids
;
268 * Show a deleted file version requested by the visitor.
269 * TODO Mostly copied from Special:Undelete. Refactor.
271 protected function tryShowFile( $archiveName ) {
272 global $wgOut, $wgRequest, $wgUser, $wgLang;
274 $repo = RepoGroup
::singleton()->getLocalRepo();
275 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
277 // Check if user is allowed to see this file
278 if ( !$oimage->exists() ) {
279 $wgOut->addWikiMsg( 'revdelete-no-file' );
282 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
283 if( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
284 $wgOut->permissionRequired( 'suppressrevision' );
286 $wgOut->permissionRequired( 'deletedtext' );
290 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
291 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
292 $this->targetObj
->getText(),
293 $wgLang->date( $oimage->getTimestamp() ),
294 $wgLang->time( $oimage->getTimestamp() ) );
296 Xml
::openElement( 'form', array(
298 'action' => $this->getTitle()->getLocalUrl(
299 'target=' . urlencode( $oimage->getName() ) .
300 '&file=' . urlencode( $archiveName ) .
301 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
304 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
310 # We mustn't allow the output to be Squid cached, otherwise
311 # if an admin previews a deleted image, and it's cached, then
312 # a user without appropriate permissions can toddle off and
313 # nab the image, and Squid will serve it
314 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
315 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
316 $wgRequest->response()->header( 'Pragma: no-cache' );
318 # Stream the file to the client
320 require_once( "$IP/includes/StreamFile.php" );
321 $key = $oimage->getStorageKey();
322 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
323 wfStreamFile( $path );
327 * Get the list object for this request
329 protected function getList() {
330 if ( is_null( $this->list ) ) {
331 $class = $this->typeInfo
['list-class'];
332 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
338 * Show a list of items that we will operate on, and show a form with checkboxes
339 * which will allow the user to choose new visibility settings.
341 protected function showForm() {
342 global $wgOut, $wgUser, $wgLang;
345 if ( $this->typeName
== 'logging' ) {
346 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
348 $wgOut->addWikiMsg( 'revdelete-selected',
349 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
352 $wgOut->addHTML( "<ul>" );
354 $where = $revObjs = array();
358 $list = $this->getList();
359 for ( $list->reset(); $list->current(); $list->next() ) {
360 $item = $list->current();
361 if ( !$item->canView() ) {
362 if( !$this->submitClicked
) {
363 $wgOut->permissionRequired( 'suppressrevision' );
366 $UserAllowed = false;
369 $wgOut->addHTML( $item->getHTML() );
372 if( !$numRevisions ) {
373 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
377 $wgOut->addHTML( "</ul>" );
379 $this->addUsageText();
381 // Normal sysops can always see what they did, but can't always change it
382 if( !$UserAllowed ) return;
384 // Show form if the user can submit
385 if( $this->mIsAllowed
) {
386 $out = Xml
::openElement( 'form', array( 'method' => 'post',
387 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
388 'id' => 'mw-revdel-form-revisions' ) ) .
389 Xml
::fieldset( wfMsg( 'revdelete-legend' ) ) .
390 $this->buildCheckBoxes() .
391 Xml
::openElement( 'table' ) .
393 '<td class="mw-label">' .
394 Xml
::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
396 '<td class="mw-input">' .
397 Xml
::listDropDown( 'wpRevDeleteReasonList',
398 wfMsgForContent( 'revdelete-reason-dropdown' ),
399 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
403 '<td class="mw-label">' .
404 Xml
::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
406 '<td class="mw-input">' .
407 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason' ) ) .
411 '<td class="mw-submit">' .
412 Xml
::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
413 array( 'name' => 'wpSubmit' ) ) .
416 Xml
::closeElement( 'table' ) .
417 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
418 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
419 Xml
::hidden( 'type', $this->typeName
) .
420 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
421 Xml
::closeElement( 'fieldset' ) . "\n";
425 if( $this->mIsAllowed
) {
426 $out .= Xml
::closeElement( 'form' ) . "\n";
427 // Show link to edit the dropdown reasons
428 if( $wgUser->isAllowed( 'editinterface' ) ) {
429 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'revdelete-reason-dropdown' );
430 $link = $wgUser->getSkin()->link(
432 wfMsgHtml( 'revdelete-edit-reasonlist' ),
434 array( 'action' => 'edit' )
436 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
439 $wgOut->addHTML( $out );
443 * Show some introductory text
444 * FIXME Wikimedia-specific policy text
446 protected function addUsageText() {
447 global $wgOut, $wgUser;
448 $wgOut->addWikiMsg( 'revdelete-text' );
449 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
450 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
452 if( $this->mIsAllowed
) {
453 $wgOut->addWikiMsg( 'revdelete-confirm' );
458 * @return String: HTML
460 protected function buildCheckBoxes() {
464 // If there is just one item, use checkboxes
465 $list = $this->getList();
466 if( $list->length() == 1 ) {
468 $bitfield = $list->current()->getBits(); // existing field
469 if( $this->submitClicked
) {
470 $bitfield = $this->extractBitfield( $this->extractBitParams($wgRequest), $bitfield );
472 foreach( $this->checks
as $item ) {
473 list( $message, $name, $field ) = $item;
474 $innerHTML = Xml
::checkLabel( wfMsg($message), $name, $name, $bitfield & $field );
475 if( $field == Revision
::DELETED_RESTRICTED
)
476 $innerHTML = "<b>$innerHTML</b>";
477 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
478 $html .= "<tr>$line</tr>\n";
480 // Otherwise, use tri-state radios
483 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-same').'</th>';
484 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
485 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</th>';
486 $html .= "<th></th></tr>\n";
487 foreach( $this->checks
as $item ) {
488 list( $message, $name, $field ) = $item;
489 // If there are several items, use third state by default...
490 if( $this->submitClicked
) {
491 $selected = $wgRequest->getInt( $name, 0 /* unchecked */ );
493 $selected = -1; // use existing field
495 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
496 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
497 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
498 $label = wfMsgHtml($message);
499 if( $field == Revision
::DELETED_RESTRICTED
) {
500 $label = "<b>$label</b>";
502 $line .= "<td>$label</td>";
503 $html .= "<tr>$line</tr>\n";
512 * UI entry point for form submission.
513 * @param $request WebRequest
515 protected function submit( $request ) {
516 global $wgUser, $wgOut;
517 # Check edit token on submission
518 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
519 $wgOut->addWikiMsg( 'sessionfailure' );
522 $bitParams = $this->extractBitParams( $request );
523 $listReason = $request->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
524 $comment = $listReason;
525 if( $comment != 'other' && $this->otherReason
!= '' ) {
526 // Entry from drop down menu + additional comment
527 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason
;
528 } elseif( $comment == 'other' ) {
529 $comment = $this->otherReason
;
531 # Can the user set this field?
532 if( $bitParams[Revision
::DELETED_RESTRICTED
]==1 && !$wgUser->isAllowed('suppressrevision') ) {
533 $wgOut->permissionRequired( 'suppressrevision' );
536 # If the save went through, go to success message...
537 $status = $this->save( $bitParams, $comment, $this->targetObj
);
538 if ( $status->isGood() ) {
541 # ...otherwise, bounce back to form...
543 $this->failure( $status );
549 * Report that the submit operation succeeded
551 protected function success() {
553 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
554 $wgOut->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo
['success'] );
555 $this->list->reloadFromMaster();
560 * Report that the submit operation failed
562 protected function failure( $status ) {
564 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
565 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
570 * Put together an array that contains -1, 0, or the *_deleted const for each bit
571 * @param $request WebRequest
574 protected function extractBitParams( $request ) {
576 foreach( $this->checks
as $item ) {
577 list( /* message */ , $name, $field ) = $item;
578 $val = $request->getInt( $name, 0 /* unchecked */ );
579 if( $val < -1 ||
$val > 1) {
580 $val = -1; // -1 for existing value
582 $bitfield[$field] = $val;
584 if( !isset($bitfield[Revision
::DELETED_RESTRICTED
]) ) {
585 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
591 * Put together a rev_deleted bitfield
592 * @param $bitPars array extractBitParams() params
593 * @param $oldfield int current bitfield
596 public static function extractBitfield( $bitPars, $oldfield ) {
597 // Build the actual new rev_deleted bitfield
599 foreach( $bitPars as $const => $val ) {
601 $newBits |
= $const; // $const is the *_deleted const
602 } else if( $val == -1 ) {
603 $newBits |
= ($oldfield & $const); // use existing
610 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
612 protected function save( $bitfield, $reason, $title ) {
613 return $this->getList()->setVisibility(
614 array( 'value' => $bitfield, 'comment' => $reason )